package biz.yfsoft.app.fastframework.admin;

import java.io.File;
import java.io.FilenameFilter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import biz.yfsoft.app.fastframework.kit.DateKit;
import biz.yfsoft.app.fastframework.kit.RuntimeKit;

import com.jfinal.kit.PropKit;

public class DataController extends AdminController {
	
	private boolean isLinux = false;
	
	{
		isLinux = RuntimeKit.isLinux();
	}
	public void backup() {
		render("/admin/backup.jsp");
	}

	//进行数据库的备份
	public void doBackup(){
		PropKit.use("config.txt");
		String user = PropKit.get("db.user");
		String pass = PropKit.get("db.pass");
		String host = PropKit.get("db.host");
		String port = PropKit.get("db.port");
		String name = PropKit.get("db.name");
		String dir = PropKit.get(isLinux?"db.linux.exportdir":"db.win.exportdir");
        String cmd = "mysqldump --user={0} --port={1} --host={2} --password={3} --opt {4} > {5}fast_{6}.bak";
        cmd = MessageFormat.format(cmd, user,port,host,pass,name,dir,DateKit.toStr(new Date(), "yyyyMMddHHmmss"));
        if(isLinux?RuntimeKit.exec(new String[]{"sh","-c",cmd}):RuntimeKit.exec(cmd)){
        	renderJson(wrapJson(0));
        }else{
        	renderJson(wrapJson(-1,"备份指令执行失败"));
        }
		
	}
	
	public void restore() {
		//读取文件
		PropKit.use("config.txt");
		String dir;
		if(RuntimeKit.isLinux()){
			dir = PropKit.get("db.linux.exportdir");
		}else{
			dir = PropKit.get("db.win.exportdir");
		}
		java.io.File fdir = new java.io.File(dir);
		if(!fdir.exists()){
			//导出的配置文件设置错误
			render("/admin/restore.jsp");
			return;
		}
		java.io.File[] files = fdir.listFiles(new FilenameFilter(){

			@Override
			public boolean accept(File arg0, String arg1) {
				return (arg1.startsWith("fast_")&&arg1.endsWith(".bak"));
			}
			
		});
		List<Map<String,String>> fileList = new ArrayList<Map<String,String>>();
		Map<String,String> map;
		for(File f:files){
			map = new HashMap<String,String>();
			map.put("name", f.getName());
			map.put("createtime", DateKit.toStr(new Date(f.lastModified())));
			fileList.add(map);
		}
		setAttr("backupList", fileList);
		render("/admin/restore.jsp");
	}
	
	public void doRestore() {
		String sqlFile = getPara("name");
		PropKit.use("config.txt");
		String user = PropKit.get("db.user");
		String pass = PropKit.get("db.pass");
		String host = PropKit.get("db.host");
		String port = PropKit.get("db.port");
		String name = PropKit.get("db.name");
		String dir = PropKit.get(isLinux?"db.linux.exportdir":"db.win.exportdir");
        String cmd = "mysql  -h{0} -P{1} -u{2} -p{3} {4} < {5}{6}";   
        
        cmd = MessageFormat.format(cmd, host,port,user,pass,name,dir,sqlFile);
        if(isLinux?RuntimeKit.exec(new String[]{"sh","-c",cmd}):RuntimeKit.exec(cmd)){
        	renderJson(wrapJson(0));
        }else{
        	renderJson(wrapJson(-1,"还原指令执行失败"));
        }
	}
	
	public void download() {
		String sqlFile = getPara("name");
		PropKit.use("config.txt");
		String dir = PropKit.get(isLinux?"db.linux.exportdir":"db.win.exportdir");
		renderFile(new File(dir+sqlFile));
	}
}
